package com.youxuit.youxu.chain;

import lombok.Data;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Data
@Component
public class PluginChain {

    /**
     * 责任链
     */
    public List<Plugin> pluginList;

    public PluginChain(List<Plugin> pluginList) {
        this.pluginList = pluginList.stream().sorted(Comparator.comparing(Plugin::getOrder)).collect(Collectors.toList());
    }

    /**
     * 责任链执行逻辑
     *
     * @param exchange
     * @param PluginMateDto
     */
    public void execute(ServerWebExchange exchange, PluginMateDto PluginMateDto) {
        pluginList.forEach(plugin -> plugin.execute(exchange, PluginMateDto));
    }
}
